package org.edu.modules.logistics.good.service.impl;

import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.DateUtil;
import org.edu.modules.logistics.base.dto.VeHqCangkuDto;
import org.edu.modules.logistics.base.service.VeHqBgwpflService;
import org.edu.modules.logistics.base.service.VeHqFileService;
import org.edu.modules.logistics.good.mapper.VeHqWupinMapper;
import org.edu.modules.logistics.good.entity.VeHqWupin;
import org.edu.modules.logistics.good.dto.VeHqWupinDto;
import org.edu.modules.logistics.good.service.VeHqWupinService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeHqWupinServiceImpl extends BaseServiceImpl<VeHqWupin, VeHqWupinMapper> implements VeHqWupinService {

    @Autowired
    VeHqBgwpflService veHqBgwpflService;

    @Autowired
    private VeHqFileService veJkyFileService;

    public IPage<VeHqWupinDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeHqWupinDto findDtoById(String id) {
        VeHqWupin entity = super.findById(id);
        VeHqWupinDto dto = ConvertUtils.convertToDto(entity, VeHqWupinDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeHqWupinDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            super.update(dto);
        }
        veJkyFileService.updateFiles(dto.getFileIds(),dto.getId()+"","veHqWupin");
    }



    @Override
    public void importExcel(List<Map<String, Object>> mapList) throws Exception {
        for (int i = 1; i < mapList.size(); i++) {
            Map map= mapList.get(i);
            VeHqWupinDto dto = new VeHqWupinDto();

            dto.setCode((String)map.get(0));
            dto.setName((String)map.get(1));

            String flmc = (String)map.get(2);
            Integer pid = veHqBgwpflService.findPid(flmc);
            if(pid==null){
                throw new Exception("未找到分类");
            }
            Float aFloat = (Float) map.get(3);
            BigDecimal calculation = new BigDecimal(Float.toString(aFloat));
            dto.setWpdj(calculation);

            String status = (String)map.get(4);
            dto.setStatus("启用".equals(status)?1:0);
            dto.setJhsynx((String)map.get(5));
            dto.setWppp((String)map.get(6));
            String serstatus = (String)map.get(7);
            dto.setUseStatus("正常".equals(serstatus)?1:0);

            String shifou = (String)map.get(8);
            dto.setSfzj("是".equals(serstatus)?1:0);

            String date = (String)map.get(9);
            dto.setGzTime(DateUtil.parseDate(date));

            dto.setRemark((String)map.get(10));


            this.insertOrUpdate(dto,true);
        }
    }
}
